﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.Unity;
using Web.SystemConfig.Services;
using System.Data;

namespace Web.Views.Public
{
    public partial class SystemErrorLog : PageBase
    {
        [Dependency]
        public IAuth_User UserComponent
        {
            get;
            set;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
        }

        /// <summary>
        /// 查询日志
        /// </summary>
        private DataTable GetData()
        {
            string sql = "select * from oracle_entlog where Category='SMErrorLog' ";
            if (!string.IsNullOrEmpty(this.txtContent1.Text.Trim()))
            {
                sql += " and Message like '%" + this.txtContent1.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(this.txtContent2.Text.Trim()))
            {
                sql += " and Message like '%" + this.txtContent2.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(this.txtContent3.Text.Trim()))
            {
                sql += " and Message like '%" + this.txtContent3.Text.Trim() + "%'";
            }
            sql += " order by timestamps desc";
            IMDataHelper dal = new IMDataHelper();
            DataSet ds = dal.DBHelper.ExecuteDataSet(sql);
            if (ds != null && ds.Tables.Count > 0)
            {
                return ds.Tables[0];
            }
            else
            {
                return null;
            }
        }

        private void BindGrid()
        {
            DataTable dt = this.GetData();
            if (dt != null)
            {
                pager.RecordCount = dt.Rows.Count;
            }
            else
            {
                pager.RecordCount = 0;
            }
            this.gv_Log.DataSource = PageDataList.GetPagedTable(dt, pager.CurrentPageIndex, pager.PageSize);
            this.gv_Log.DataBind();
        }

        protected void pager_PageChanging(object sender, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            this.pager.CurrentPageIndex = e.NewPageIndex;
            BindGrid();
        }

        protected void gv_Log_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView drv = (DataRowView)e.Row.DataItem;
                string message = drv["Message"].ToString();
                string[] arr = message.Split(new string[] { "@@@" }, StringSplitOptions.RemoveEmptyEntries);
                string log_TypeId = arr[0];
                string log_Msg = arr[1];
                e.Row.Cells[1].Text = GetLogTypeString.GetLogType(log_TypeId);
                e.Row.Cells[0].Text = log_Msg;
                e.Row.Cells[2].Text = drv["Category"].ToString() == "SMErrorLog" ? "异常日志" : "异常日志";
            }
        }

        protected void btn_search_Click(object sender, EventArgs e)
        {
            try
            {
                this.pager.CurrentPageIndex = 0;
                BindGrid();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex.Message);
                Alert(ex.Message);
            }
        }
    }
}